﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Manager.DataAccess;
using Manager.App_Code;
using System.Linq;
using Manager.Common;


namespace Manager.FormMain
{
    public partial class Frm_RoomDiagram : DevExpress.XtraEditors.XtraForm,IDatatranfer
    {
        Department[] deps = new Department[0];
        List<Department> deps1 = null;
        List<Department> deps2 = null;
        List<Control> lstControl = null;
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();
    
        public Frm_RoomDiagram()
        {
            InitializeComponent();
            
        }

        public void Frm_RoomDiagram_Load(object sender, EventArgs e)
        {
            ShowDepartments();
            AddEvent();
        }

        private void AddEvent()
        {
            lstControl = new List<Control>();
            lstControl = CommonFunction.GetListRoomByName(this, "butRoom", lstControl);
            if (lstControl.Count > 0)
            {
                foreach (var control in lstControl)
                {
                    var btnRoom = control.Parent as Room;
                    if (btnRoom != null)
                        btnRoom.DataChanged += btnRoom_DataChanged;
                }
            }
        }

        private void btnRoom_DataChanged(object sender, DataChangedEventArgs e)
        {
            var frm = sender as Room;
            if (frm != null)
            {
                frm.Room_Load(null, null);
                //AddEvent();
            }
        }

        public void ShowDepartments()
        {
            panelLeft.Controls.Clear();
            panelRight.Controls.Clear();
            LoadDepartments();            
            panelLeft.SuspendLayout();
            for (int i = 0; i < deps2.Count; ++i)
            {
                Department d2 = deps2[i];
                d2.Location = new System.Drawing.Point(1, 20 + i * 69);
                d2.ShowRooms();
            }
            panelLeft.Controls.AddRange(deps2.ToArray());
            panelLeft.ResumeLayout();

            panelRight.SuspendLayout();
            for (int i = 0; i < deps1.Count; ++i)
            {
                Department d1 = deps1[i];
                d1.Location = new System.Drawing.Point(1, 20 + i * 69);
                d1.ShowRooms();
            }
            panelRight.Controls.AddRange(deps1.ToArray());
            panelRight.ResumeLayout();            
        }

        private void LoadDepartments()
        {            
            var listTang = db.Tangs.ToList();
            var count = (int)(listTang.Count / 2);
            deps = new Department[listTang.Count];
            deps1 = new List<Department>();
            deps2 = new List<Department>();
            for (int i = 0; i < listTang.Count; ++i)
            {
                deps[i] = new Department();
                deps[i].ID = listTang[i].MaTang;
                deps[i].Name = listTang[i].TenTang;
                deps[i].Position = listTang[i].ViTri.Value;
                if (i < count)
                    deps2.Add(deps[i]);
                else
                    deps1.Add(deps[i]);
            }            
        }

        private void ThemTangToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmAddDep frmAddDep = new FrmAddDep();
            DialogResult dr = frmAddDep.ShowDialog();
            if (dr == DialogResult.OK)
            {
                ShowDepartments();
            }
        }

        private void LamMoiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowDepartments();
        }        

        public void TranferData(Phong dr)
        {
            lblPhong.Text = dr.TenPhong;
            lblGhichuPhong.Text = "";
            var loaiPhong = db.LoaiPhongs.Single(i => i.MaLoaiPhong == dr.LoaiPhong);
            var tienTe = db.TienTes.Single(i => i.MaDonVi == loaiPhong.DonViTienTe);
            lblGia.Text = loaiPhong.GiaPhong.ToString() + " " + tienTe.TenDonVi;
            lblKhachHang.Text = "";
            lblLoaiPhong.Text = "";
            lblNhom.Text = "";
            lblSoKhach.Text = "";
            lblThoigianDen.Text = "";
            lblThoiGianDi.Text = "";            
        }

        private void _splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void butRefresh_Click(object sender, EventArgs e)
        {
            Frm_RoomDiagram_Load(sender, e);
        }

        public void TranferData(Phong dr, PhieuDangKyPhong phieuDangKyPhong)
        {
            try
            {
                if (phieuDangKyPhong != null)
                {
                    lblPhong.Text = dr.TenPhong;
                    lblGhichuPhong.Text = phieuDangKyPhong.GhiChu;
                    var loaiPhong = db.LoaiPhongs.Single(i => i.MaLoaiPhong == dr.LoaiPhong);
                    var tienTe = phieuDangKyPhong.ChiTietPhieuDangKy.TienTe;
                    lblGia.Text = phieuDangKyPhong.ChiTietPhieuDangKy.GiaPhong + "  " + tienTe.TenDonVi;
                    var khachHang = db.KhachHangs.Single(i => i.MaKH == phieuDangKyPhong.MaKhachHang);
                    lblKhachHang.Text = khachHang.TenKH;
                    lblLoaiPhong.Text = loaiPhong.TenLoaiPhong;
                    var nhomKhach = db.NhomKhaches.SingleOrDefault(i => i.MaNhomKhach == khachHang.MaNhomKhach);
                    lblNhom.Text = nhomKhach.TenNhomKhach;
                    lblSoKhach.Text = phieuDangKyPhong.SoNguoi.ToString();
                    lblThoigianDen.Text = phieuDangKyPhong.NgayDen.Value.ToShortDateString();
                    lblThoiGianDi.Text = phieuDangKyPhong.NgayDi.Value.ToShortDateString();
                }
                else
                {
                    lblPhong.Text = dr.TenPhong;
                    lblGhichuPhong.Text = dr.GhiChu;
                    var loaiPhong = db.LoaiPhongs.Single(i => i.MaLoaiPhong == dr.LoaiPhong);
                    var tienTe = db.TienTes.Single(i => i.MaDonVi == loaiPhong.DonViTienTe);
                    lblGia.Text = loaiPhong.GiaPhong.ToString() + "  " + tienTe.TenDonVi;
                    lblKhachHang.Text = "Không có";
                    lblLoaiPhong.Text = loaiPhong.TenLoaiPhong;
                    lblNhom.Text = "";
                    lblSoKhach.Text = "0";
                    lblThoigianDen.Text = "";
                    lblThoiGianDi.Text = "";
                }
            }
            catch (Exception ex)
            {
                Log.WriteLog("Frm_RoomDiagram:TranferData" + ex.Message + "\n" + ex.StackTrace);
                MessageBox.Show("Có lỗi xảy ra!!!");
            }
        }
    }
}